#ifndef BLOG_H
#define BLOG_H 1

#include "builtins.h"


/* blog param's */
#define BLOG_VAR_NAME "blog"
#define BLOG_URI BLOG_VAR_NAME
#define BLOG_PATH BLOG_VAR_NAME
#define BLOG_DB_PRFIX BLOG_VAR_NAME
#define BLOG_TMPL_DEF BLOG_VAR_NAME

/* blog attributes */
#define BLOG_REQ_LINK "link"
#define BLOG_ACTION "action"
#define BLOG_ACTION_SHOW "show"
#define BLOG_ACTION_CONTENT "content"
#define BLOG_LIMIT "limit"
#define BLOG_LIMIT_DEF "10"

/* SQL statement's */
#define BLOG_SELECT_TYPES { db_int }
#define BLOG_SELECT "SELECT posts.id, posts.title, datetime(posts.date, 'unixepoch', 'localtime') as date, posts.link, author.aname, author.uname, category.nname, category.cname, pubdate, views, html FROM posts, author, category WHERE category.id=posts.cat_id AND posts.published=1 ORDER BY date DESC LIMIT ?1"
#define BLOG_SELECT_CONTENT_TYPES { db_str }
#define BLOG_SELECT_CONTENT "SELECT posts.id, posts.title, datetime(posts.date, 'unixepoch', 'localtime') as date, posts.link, author.aname, author.uname, category.nname, category.cname, pubdate, views, html FROM posts, author, category WHERE category.id=posts.cat_id AND link=?1"
#define BLOG_CONTENT_VIEWS_TYPES { db_str }
#define BLOG_CONTENT_VIEWS "UPDATE posts SET (SELECT views FROM posts)+1 WHERE link=?1"
#define BLOG_INSERT_TYPES { db_str, db_str, db_str, db_uint, db_str, db_str, db_str }
#define BLOG_INSERT "INSERT OR REPLACE INTO posts (link, title, html, date, author_id, cat_id) VALUES (?1, trim(?2, ' \t\r\n'), ?3, ?4, (SELECT id FROM author WHERE uname=?5), (SELECT id FROM category WHERE cname=?6))"
#define BLOG_PUB_TYPES { db_str }
#define BLOG_UNPUBLISH "UPDATE posts SET published=0,pubdate=0 WHERE link=?1"
#define BLOG_PUBLISH "UPDATE posts SET published=1,pubdate=strftime('%s', 'now') WHERE link=?1"
#define BLOG_CATG_ADD_TYPES { db_str, db_str }
#define BLOG_CATG_ADD "INSERT OR REPLACE INTO category (id, nname, cname) VALUES ((SELECT id FROM category WHERE cname=?2), ?1, ?2)"
#define BLOG_TAG_ADD_TYPES { db_str, db_str }
#define BLOG_TAG_ADD "INSERT OR REPLACE INTO tag (id, cname, nname) VALUES ((SELECT id FROM tag WHERE cname=?1), ?1, ?2)"
#define BLOG_TAG_REF_TYPES { db_str }
#define BLOG_TAG_INC_REF "UPDATE tag SET (SELECT refs FROM tag)+1 WHERE cname=?1"
#define BLOG_TAG_DEC_REF "UPDATE tag SET (SELECT refs FROM tag)-1 WHERE cname=?1"
#define BLOG_TAG_POST_ADD_TYPES { db_str, db_str }
#define BLOG_TAG_POST_ADD "INSERT OR REPLACE INTO tag_post (post_link, tag_cname) VALUES (?1, ?2)"
#define BLOG_COMMENT_ADD_TYPES { db_str, db_str, db_str, db_str, db_uint }
#define BLOG_COMMENT_ADD "INSERT INTO comments (post_id, author, url, comment, date) VALUES ((SELECT id FROM posts WHERE link=?1), ?2, ?3, ?4, ?5)"
#define BLOG_COMMENT_SHOW_TYPES { db_int, db_int, db_int }
#define BLOG_COMMENT_SHOW "SELECT author, url, date, comment FROM comments WHERE post_id=?1 AND date>=?2 LIMIT ?3"
#define BLOG_CREATE_TABLES \
                "CREATE TABLE IF NOT EXISTS tag (" \
                    "id INTEGER PRIMARY KEY, " \
                    "nname TEXT NOT NULL, " \
                    "cname TEXT NOT NULL UNIQUE, " \
                    "refs UNSIGNED INTEGER DEFAULT 0); "\
                "CREATE TABLE IF NOT EXISTS category (" \
                    "id INTEGER PRIMARY KEY AUTOINCREMENT," \
                    "nname TEXT DEFAULT NULL," \
                    "cname TEXT NOT NULL UNIQUE); " \
                "CREATE TABLE IF NOT EXISTS author " \
                    "(id INTEGER PRIMARY KEY AUTOINCREMENT," \
                    "aname TEXT NOT NULL," \
                    "uname TEXT NOT NULL UNIQUE); " \
                "INSERT OR REPLACE INTO author (aname, uname) VALUES ('Administrator','admin'); " \
                "CREATE TABLE IF NOT EXISTS posts " \
                    "(id INTEGER PRIMARY KEY AUTOINCREMENT," \
                    "link TEXT NOT NULL UNIQUE," \
                    "title TEXT NOT NULL," \
                    "html TEXT NOT NULL," \
                    "date UNSIGNED INTEGER," \
                    "author_id INTEGER REFERENCES author(id) ON DELETE CASCADE ON UPDATE CASCADE," \
                    "cat_id INTEGER REFERENCES category(id) ON DELETE CASCADE ON UPDATE CASCADE," \
                    "published CHAR DEFAULT '0'," \
                    "pubdate UNSIGNED INTEGER DEFAULT 0," \
                    "views UNSIGNED INTEGER DEFAULT 0); " \
                "CREATE TABLE IF NOT EXISTS comments (" \
                    "id INTEGER PRIMARY KEY, " \
                    "post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE ON UPDATE CASCADE, " \
                    "author TEXT NOT NULL, " \
                    "url TEXT, " \
                    "date INTEGER, " \
                    "comment TEXT NOT NULL); " \
                "CREATE TABLE IF NOT EXISTS tag_post (" \
                    "tag_cname TEXT NOT NULL REFERENCES tag(cname) ON DELETE CASCADE ON UPDATE CASCADE, " \
                    "post_link TEXT NOT NULL REFERENCES posts(link) ON DELETE CASCADE ON UPDATE CASCADE, " \
                    "PRIMARY KEY (tag_cname, post_link));"


int
__blog_preinit(void);

int
__blog_init(void);

int
__blog_free(void);

void
__blog(const struct var_reqdata *vr);

#endif
